import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    user: {
      userId: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).userId,
      nickname: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).nickname,
      imageUrl: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).imageUrl,
      signature: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).signature,
      email: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).email,
      phone: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).phone,
      gender: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).gender,
      office: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).office
    },
    web: {
      title: window.localStorage.getItem('web' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('web' || '[]')).userId,
      title2: window.localStorage.getItem('web' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('web' || '[]')).title2,
      logo: window.localStorage.getItem('web' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('web' || '[]')).logo,
      isRegister: window.localStorage.getItem('web' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('web' || '[]')).isRegister,
      message: window.localStorage.getItem('web' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('web' || '[]')).message,
      newUserRole: window.localStorage.getItem('web' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('web' || '[]')).newUserRole
    },
    ossData: {
      host: 'http://cdn.404zz.cn'
    }
  },
  mutations: {
    login (state, user) {
      state.user = user
      window.localStorage.setItem('user', JSON.stringify(user))
    },
    setWebInfo (state, web) {
      state.web = web
      window.localStorage.setItem('web', JSON.stringify(web))
    },
    setImage (state, imageUrl) {
      state.user.imageUrl = imageUrl
      setLocalStorage(imageUrl, 1)
    },
    setEmail (state, email) {
      state.user.email = email
      setLocalStorage(email, 2)
    },
    setPhone (state, phone) {
      state.user.phone = phone
      setLocalStorage(phone, 3)
    },
    setInfo (state, user) {
      state.user.nickname = user.nickname
      state.user.office = user.office
      state.user.gender = user.gender
      state.user.signature = user.signature
      setLocalStorage(user, 4)
    },
    quitLogin (state) {
      state.user = {}
      window.localStorage.clear()
    },
    setOssData (state, host) {
      state.ossData.host = host
      window.localStorage.setItem('host', host)
    }
  }
})

/**
 * @param str
 * @param type 1 用户图片 2：用户邮箱 3：用户手机号 4:更新用户基本信息
 */
function setLocalStorage (str, type) {
  var user = JSON.parse(window.localStorage.getItem('user'))
  // eslint-disable-next-line eqeqeq
  if (type == 1) {
    user.imageUrl = str
    // eslint-disable-next-line eqeqeq
  } else if (type == 2) {
    user.email = str
    // eslint-disable-next-line eqeqeq
  } else if (type == 3) {
    user.phone = str
    // eslint-disable-next-line eqeqeq
  } else if (type == 4) {
    user.nickname = str.nickname
    user.office = str.office
    user.gender = str.gender
    user.signature = str.signature
  }
  window.localStorage.setItem('user', JSON.stringify(user))
}
